Conditions | 1 |
Paths | 24 |
Total Lines | 106 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 21 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var TableHelper = { |
||
261 | function initInstance(){ |
||
262 | // Singleton |
||
263 | // Private methods and variables |
||
264 | /*function BuildRequest(rq, crntTableId, strDesc){ |
||
265 | rq.tableId = crntTableId; |
||
266 | TableHelper.BuildRequest.Sort(rq, strDesc); |
||
267 | TableHelper.BuildRequest.Filter(rq); |
||
268 | };*/ |
||
269 | var BuildRequest = TableHelper.BuildRequest.Run; |
||
270 | var tail = null; |
||
271 | function LoadData(tableContainer, rq){ |
||
272 | if(tail!==null){ tail.abort();} |
||
273 | TableHelper.LoadData.SetVisability(tableContainer, false); |
||
274 | var xmlhttp = window.XMLHttpRequest ? |
||
275 | new XMLHttpRequest() : /* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
276 | new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
277 | xmlhttp.onreadystatechange = function(){ |
||
278 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
279 | var d = JSON.parse(xmlhttp.responseText); |
||
280 | TableHelper.Draw.Run(tableContainer, d, instance); |
||
281 | TableHelper.LoadData.SetVisability(tableContainer, true); |
||
282 | instance.LoadEndCalback(tableContainer); |
||
283 | } |
||
284 | }; |
||
285 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
286 | xmlhttp.send(); |
||
287 | tail = xmlhttp; //put at tail to can abort later any previous |
||
288 | } |
||
289 | function ReloadData(tableId){ |
||
290 | var request = {}; |
||
291 | BuildRequest(request, tableId, this.strDesc); |
||
292 | LoadData(tableId, request); |
||
293 | } |
||
294 | |||
295 | var GoPageGetNo = TableHelper.GoPage.GetNo; |
||
296 | var getParent = TableHelper.GetParent; |
||
297 | var RequestToUrl = TableHelper.RequestToUrl; |
||
298 | |||
299 | return { |
||
300 | rq: null, |
||
301 | strAsc: String.fromCharCode(9650), //▲ |
||
302 | strDesc: String.fromCharCode(9660),//▼ |
||
303 | ColumnHover: TableHelper.ColumnHover, //function(tableContainer, index) |
||
304 | Export: function(lnk, eType){ |
||
305 | var request = {}; |
||
306 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
307 | BuildRequest(request, crntTableId, this.strDesc); |
||
308 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
309 | eType : |
||
310 | "csv"; |
||
311 | window.open(RequestToUrl(request)); |
||
312 | return false; |
||
313 | }, |
||
314 | Filter: function(field){ |
||
315 | var crntTableId = TableHelper.Filter.GetTableId(field); |
||
316 | if(crntTableId !== null){ |
||
317 | var request = {}; |
||
318 | var exRq = this.rq; |
||
319 | BuildRequest(request, crntTableId, this.strDesc); |
||
320 | if(exRq === null || |
||
321 | request.filter !== exRq.filter || |
||
322 | request.filterBy !== exRq.filterBy |
||
323 | ){ |
||
324 | LoadData(crntTableId, request); |
||
325 | } |
||
326 | } |
||
327 | }, |
||
328 | GoPage: function(lnk){ |
||
329 | var request = {}; |
||
330 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
331 | BuildRequest(request, crntTableId, this.strDesc); |
||
332 | request.pageNo = GoPageGetNo(lnk, crntTableId); |
||
333 | LoadData(crntTableId, request); |
||
334 | return false; |
||
335 | }, |
||
336 | init: function (tableId){ |
||
337 | var tContainer = document.getElementById(tableId); |
||
338 | if(!TableHelper.IePrior(9)){ |
||
339 | TableHelper.Init.SetColumnsHoverEffect(tContainer, tableId); |
||
340 | } |
||
341 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
342 | TableHelper.ProcessPaginationLinks(tfoot); |
||
343 | }, |
||
344 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
345 | ReloadData: ReloadData, |
||
346 | Sort: function(colNo, lnk){ |
||
347 | var request = {}; |
||
348 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
349 | BuildRequest(request, crntTableId, this.strDesc); |
||
350 | if(Math.round(colNo) === request.colNo){ |
||
351 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
352 | }else{ |
||
353 | request.colNo = Math.round(colNo); |
||
354 | request.colOrd = "asc"; |
||
355 | } |
||
356 | LoadData(crntTableId, request); |
||
357 | /* Clear and add new sort arrow */ |
||
358 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
359 | var length = headSpans.length; |
||
360 | for(var i = 0; i < length; i++){ |
||
361 | headSpans[i].innerHTML = ""; |
||
362 | } |
||
363 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
364 | } |
||
365 | }; |
||
366 | } |
||
367 | return { |
||
378 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: